package com.china08.yxyapi.api.home.index.kecheng;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroGrade;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLesson;
import com.china08.yxyapi.entity.db.res.YxyResSubject;
import com.china08.yxyapi.entity.db.res.YxyResVersion;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroGradeRepository;
import com.china08.yxyapi.repository.db.YxyResSubjectRepository;
import com.china08.yxyapi.repository.db.YxyResVersionRepository;
import com.china08.yxyapi.service.db.YxyKeChengService;

/**
 * 首页--课程查询
 * 
 */
@CrossOrigin
@RestController
public class IndexSrhKeChengController extends YxyApiBaseController {

    @Autowired
    private YxyKeChengService yxyKeChengService;

    @Autowired
    private YxyLessonsSynchroCatalogRepository yxyLessonsSynchroCatalogRepository;

    /** 学科 */
    @Autowired
    private YxyResSubjectRepository yxyResSubjectRepository;

    /** 版本 */
    @Autowired
    private YxyResVersionRepository yxyResVersionRepository;

    /** 分类 */
    @Autowired
    private YxyLessonsSynchroGradeRepository yxyLessonsSynchroGradeRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/home/index/kecheng/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody(required = false) RepModel reqModel) {

        Integer status = reqModel.status;

        List<RespModel> list = new ArrayList<RespModel>();
        RespModel model = null;
        // 同步课程
        if (status == 1) {
            List<YxyLessonsSynchroLesson> yLessonsSynchroLessons = yxyKeChengService.findIndexSynchroLesson();
            for (YxyLessonsSynchroLesson yxyLessonsSynchroLessons : yLessonsSynchroLessons) {
                model = new RespModel();

                // 学年，学科，版本
                String catalogId = yxyLessonsSynchroLessons.getCatalogId();
                YxyLessonsSynchroCatalog yxyLessonsSynchroCatalogs = yxyLessonsSynchroCatalogRepository
                        .findById(catalogId);
                String gradeId1 = yxyLessonsSynchroCatalogs.getGradeId();
                String versionId1 = yxyLessonsSynchroCatalogs.getVersionId();
                String subjectId1 = yxyLessonsSynchroCatalogs.getSubjectId();

                YxyLessonsSynchroGrade yxyLessonsSynchroGrades = yxyLessonsSynchroGradeRepository.findById(gradeId1);
                YxyResSubject yxyResSubjects = yxyResSubjectRepository.findById(subjectId1);
                YxyResVersion yxyResVersions = yxyResVersionRepository.findById(versionId1);

                model.lessonId = yxyLessonsSynchroLessons.getId();
                if (yxyLessonsSynchroGrades != null) {
                    model.gradeName = yxyLessonsSynchroGrades.getName();
                }
                model.stageId = yxyLessonsSynchroCatalogs.getStageId();
                model.subjectName = yxyResSubjects.getName();
                model.versionName = yxyResVersions.getName();
                model.vip = yxyLessonsSynchroLessons.getVip();
                model.cover = yxyLessonsSynchroLessons.getCover();
                model.count = yxyLessonsSynchroLessons.getCount();
                model.duration = yxyLessonsSynchroLessons.getDuration();
                model.name = yxyLessonsSynchroLessons.getName();
                model.ep = yxyLessonsSynchroLessons.getEp();
                model.EpTimes = yxyLessonsSynchroLessons.getEpTimes();
                model.vv = yxyLessonsSynchroLessons.getVv();
                list.add(model);
            }
        } else {
            // 专题课程
            List<YxyLessonsTopicLesson> yTopicLessons = yxyKeChengService.findIndexTopicLesson();
            for (YxyLessonsTopicLesson yxyLessonsTopicLessons : yTopicLessons) {
                model = new RespModel();
                model.stageId = "";
                model.name = yxyLessonsTopicLessons.getName();
                model.vip = yxyLessonsTopicLessons.getVip();
                model.cover = yxyLessonsTopicLessons.getCover();
                model.count = yxyLessonsTopicLessons.getCount();
                model.duration = yxyLessonsTopicLessons.getDuration();
                model.ep = yxyLessonsTopicLessons.getEp() == null ? 0.0f : yxyLessonsTopicLessons.getEp();
                model.vv = yxyLessonsTopicLessons.getVv();
                model.lessonId = yxyLessonsTopicLessons.getId();
                list.add(model);
            }

        }

        return list;
    }

    protected static class RepModel extends YxyApiReqModel {
        // 1 同步课程 0 专题课程
        public Integer status;
    }

    protected static class RespModel extends YxyApiRespModel {

        // 学年
        public String gradeName;
        // 学科
        public String subjectName;
        // 版本
        public String versionName;

        // 是否vip 0--非vip 1--vip课程
        public Integer vip;
        // 封面
        public String cover;
        // 总课时
        public Integer count;
        // 总时长
        public Integer duration;
        // 课程名称
        public String name;
        // 课程评分（所有评分之和除以评分人数即综合评分）
        public Float ep;
        // 评价人数
        public Integer EpTimes;
        // 学习人数
        public Integer vv;
        // 课程Id
        public String lessonId;
        // 学段
        public String stageId;
    }
}
